// pages/my/my.js
var network = require("../../utils/network.js")
Page({
    data: {
        wx_name: '请登录',
        user_id: '无',
        user_name: '无'
    },
    toBindWx: function(options) {
        wx.navigateTo({
            url: '/pages/bindWx/bindWx'
        })
    }
  , login: function (that) {
    //   that = this;
    wx.login({
      success: function (res) {
        if (res.code) {
          //发起网络请求
          network.request({
            url: 'https://wxqd.donting.cn/wx/login',
            data: {
              code: res.code
            },
            showLoadinTitle: '正在登陆....',
            success: function (res) {
              var data = res.data;
              if (data.code == 200) {
                that.setData({
                  sessionId: data.data.sessionId,
                  user_name: data.data.userName,
                  user_id: data.data.userNumber,
                  wx_name: data.data.userName
                })
              
              } else {
                wx.showModal({
                  title: '警告',
                  content: '请先绑定学号',
                  success(res) {
                    wx.navigateTo({
                      url: '/pages/bindWx/bindWx'
                    })
                    if (res.confirm) {
                     
                    } else if (res.cancel) {
                      console.log('用户点击取消')
                    }
                  }
                });
              }
            },
            fail: function (res) {
              console.log('网络异常---' + res)
              wx.showModal({
                title: '警告',
                content: '网络异常.....',
              });
            }
          })
        } else {
          console.log('登录失败！' + res.errMsg)
          wx.showModal({
            title: '警告',
            content: '登录失败！',
          });
        }
      },
      fail: function () {
        wx.showModal({
          title: '警告',
          content: '网络异常',
        });
      }
    });
  },
  onLoad: function (options) {
    console.log("初始化")
    this.login(this)
  },
  onShow: function (options) {
    console.log("初始化")
    if (this.data.wx_name=='请登陆'){
      this.login(this)
    }
   
  },
  toDetail:function(){
    wx.navigateTo({
      url: '/pages/detailAll/detailAll',
    })
  }
})